//
//  QHTableSubViewController.m
//  QHTableViewDemo
//
//  Created by chen on 17/3/21.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "QHTableSubViewController.h"

#import "QHReader.h"
#import "QHDecodeByAudioConverter.h"
#import "QHPlayer.h"
#import "QHConfig.h"

@interface QHTableSubViewController () <QHPlayerDelegate>

@property (nonatomic, strong) QHReader *reader;
@property (nonatomic, strong) QHDecodeByAudioConverter *decoder;
@property (nonatomic, strong) QHPlayer *player;
@property (nonatomic, strong) NSMutableData *data;

@property (nonatomic) FILE *out_file;

@end

@implementation QHTableSubViewController

- (void)dealloc {
    self.data = nil;
}

- (void)viewWillDisappear:(BOOL)animated {
    if (self.navigationController.topViewController != self) {
        [self.player close];
        [self.decoder close];
        [self.reader close];
        fclose(_out_file);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cacPath objectAtIndex:0];
    NSString *p = [cachePath stringByAppendingPathComponent:@"of.pcm"];
    const char *out_file_v = [p UTF8String];
    FILE *fp = fopen(out_file_v,"wb+");
    _out_file = fp;
    
    [QHConfig sharestanceWith:k_acc_video_44100];
    _reader = [QHReader new];
    [_reader read_config_from_aac_raw_buf:^(float sampleRate, int channels) {
        [QHConfig sharestance].sampleRate = sampleRate;
        [QHConfig sharestance].channels = channels;
    }];
    _decoder = [QHDecodeByAudioConverter new];
    _player = [QHPlayer new];
    _player.delegate = self;
    
    [_player play];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - QHPlayerDelegate

/*
 以下的还是都有不同的作用，但都是针对播放的，如果只看 AudioConverter 则可以只到这里，然后读取保存的 pcm 文件，通过 ffplay 或者其他工具播放即可。
 
 Demo 使用 renderPlay2，通用性更好
 */

- (AudioBufferList *_Nullable)renderPlay:(QHPlayer *_Nonnull)player {
    
    NSData *d = [_reader read_aac_raw_buf];
    AudioBufferList *b = [_decoder decodeAudioSamepleBuffer:d];
    
    fwrite(b->mBuffers[0].mData, 1, b->mBuffers[0].mDataByteSize, self.out_file);
    
    return b;
}

- (NSData *_Nullable)renderPlay2:(QHPlayer *_Nonnull)player size:(UInt32)size {
    if (self.data == nil) {
        self.data = [NSMutableData new];
    }
    if (self.data.length < size) {
        NSData *d = [_reader read_aac_raw_buf];
        if (d == nil) {
            return nil;
        }
        AudioBufferList *b = [_decoder decodeAudioSamepleBuffer:d];
        
        fwrite(b->mBuffers[0].mData, 1, b->mBuffers[0].mDataByteSize, self.out_file);
        
        [self.data appendBytes:b->mBuffers[0].mData length:b->mBuffers[0].mDataByteSize];
    }
    
    NSData *b = [self.data subdataWithRange:NSMakeRange(0, size)];
    
    if (self.data.length > size) {
        self.data = [[NSMutableData alloc] initWithBytes:(char *)self.data.bytes + size length:self.data.length - size];
    }
    else {
        self.data = nil;
    }
    
    return b;
}

@end
